package edu.uclmgmu.bacteriosec.aspects.java.io.BOS;

import edu.uclmgmu.bacteriosec.aspects.common.Store;

privileged aspect BufferedOutputStream_Duplicate {

	void around(java.io.BufferedOutputStream caller, byte[] a, int b, int c) : 
			call(void java.io.BufferedOutputStream.write(byte[], int, int)) && 
			target(caller) && args(a, b, c) &&
			!cflow(within(BufferedOutputStream_Duplicate)) && !within(edu.uclmgmu.bacteriosec.aspects.common.Store) 
			&& !within(junit.*) {
		String currentJoinPoint = Store.getCurrentjoinPoint();
		String signature=thisJoinPointStaticPart.getSignature().toString();
		if (currentJoinPoint.equals(signature)) {
			int numberOfCallInTurn = Store.getNumberOfCallInTurn(signature);
			int numberOfCurrentCall = Store.getNumberOfCurrentCall(signature);
			Store.updateNumberOfCurrentCall(signature);
			if (numberOfCallInTurn == numberOfCurrentCall) {
				byte[] theArray=new byte[a.length + a.length];
				int i=0;
				for (i=0; i<a.length; i++) {
					theArray[i]=a[i];
				}
				for (int j=i; j<a.length+a.length; j++) {
					theArray[j]=a[j-i];
				}
				a=theArray;
			}
			proceed(caller, a, b, c);
		} else {
			Store.updateNumberOfCurrentCall(signature);
			proceed(caller, a, b, c);
		}
	}
}
